package com.vma.jifu.offer.service.impl;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.vma.jifu.bizModel.model.*;
import com.vma.jifu.offer.service.OfferService;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chenzui on 2017/6/15.
 */
public class OfferServiceImpl implements OfferService {
    public List<Offer> qryHotOffers() {
        return Offer.dao.find("SELECT * FROM offer WHERE status = 1 AND is_hot = 1 ORDER BY create_time DESC LIMIT 5");
    }

    public List<OfferType> qryTypes() {
        return OfferType.dao.find("SELECT * FROM offer_type ORDER BY indexs ASC ");
    }

    public Page<Offer> qryOfferByTypeId(Integer cPage, Integer pSize, Integer typeId) {
        String condition="";
        List param = new ArrayList();
        if(typeId == 0){

        }else {
            condition += " AND offer_type = ?";
            param.add(typeId);
        }
        Page<Offer> offerPage = Offer.dao.paginate(cPage,pSize,
                "SELECT *",
                "FROM offer WHERE status = 1"+condition,param.toArray());
        return offerPage;
    }

    public Page<OfferComment> qyrCommentList(Integer cPage, Integer pSize, Integer offerId) {

        Page<OfferComment> commentPage = OfferComment.dao.paginate(cPage,pSize,
                "SELECT * ",
                "FROM offer_comment WHERE status = 1 AND offer_id = ? ORDER BY create_time desc",
                offerId);
        if(commentPage.getList() != null && commentPage.getList().size() > 0){
            User user = null;
            Offer offer= null;
            for (OfferComment comment:commentPage.getList()) {
                user = User.dao.findById(comment.getUserId());
                comment.put("nick_name",user.getNickName());
                comment.put("user_head_img",user.getHeadImg());
                offer= Offer.dao.findFirst("select * from offer where id = ? and status = 1 order by create_time desc",comment.getOfferId());
                comment.put("offer_name",user.getNickName());

            }
        }

        return commentPage;
    }

    public Page<OfferComment> qyrCommentList(Integer cPage, Integer pSize, String name, String mystatus, String start, String end){

        String condition="";
        List param = new ArrayList();

        if(name !=null && !"".equals(name)){
            condition += "and t.content like ? ";
            param.add("%"+name+"%");
        }
        if(mystatus !=null && !"".equals(mystatus)){
            condition += "and t.status = ? ";
            param.add(mystatus);
        }

        if(start != null && !"".equals(start)){
            condition += "and t.create_time > ? ";
            param.add(start);
        }
        if(end != null && !"".equals(end)){
            condition += "and t.create_time < ? ";
            param.add(end);
        }

        Page<OfferComment> commentPage = OfferComment.dao.paginate(cPage,pSize,
                "SELECT t.*  ", " from offer_comment t WHERE 1=1 "+condition + " order by t.id desc ",param.toArray()
                );
        if(commentPage.getList() != null && commentPage.getList().size() > 0){
            User user = null;
            Offer offer= null;
            for (OfferComment comment:commentPage.getList()) {
                user = User.dao.findById(comment.getUserId());
                comment.put("nick_name",user.getNickName());

                offer= Offer.dao.findFirst("select * from offer where id = "+comment.getOfferId()+" and status = 1");
                if(offer != null){
                    comment.put("offer_name",offer.getOfferName());
                }

            }
        }

        return commentPage;
    }

    public void submitOfferComment(Integer userId, Integer offerId, Integer orderId, String content) {
        new OfferComment().setStatus(1).setUserId(userId).setContent(content)
                .setCreateTime(new Date()).setOfferId(offerId).setUpdateTime(new Date()).save();
        OrderList.dao.findById(orderId).setStatus(5).update();
    }


    public Page<Record> qryOfferList(Integer cPage, Integer pSize, Integer typeId, String name, String status){
        String condition="";
        List param = new ArrayList();
        if(typeId == 0){

        }else {
            condition += " AND t.offer_type = ? ";
            param.add(typeId);
        }
        if(name !=null && !"".equals(name)){
            condition += "and t.offer_name like ? ";
            param.add("%"+name+"%");
        }
        if(status !=null && !"".equals(status)){
            condition += "and t.status = ? ";
            param.add(status);
        }else{
            condition += "and t.status in (0,1) ";

        }
        Page<Record> offerPage =  Db.paginate(cPage,pSize,
                "SELECT t.*,of.type_name,c.sale_num,c.sale_price ",
                "FROM offer t  left join offer_count c on c.offerid=t.id join offer_type of on t.offer_type= of.id " +
                        "WHERE   1=1   "+condition + " order by t.id desc ",param.toArray());



        for(int i=0 ; i<offerPage.getList().size();i++){
            DecimalFormat df = new DecimalFormat("0.00");

            int offer_price= offerPage.getList().get(i).get("offer_price");
            float offer_price_d= (float)  offer_price/100;
            String offersize = df.format(offer_price_d);

            offerPage.getList().get(i).set("offer_price_d",offersize );

            if(offerPage.getList().get(i).get("sale_price")!=null){
                int sale_price = offerPage.getList().get(i).get("sale_price");

                float sale_price_d= (float)  sale_price/100;

                String filesize = df.format(sale_price_d);
                offerPage.getList().get(i).set("sale_price_d",filesize );
            }


        }

        return offerPage;

    }

    public Offer getOfferById(Integer id,Integer version) {
        return Offer.dao.findById(id,version);
    }
}
